#include "./atof.h"
#include <stdlib.h>
#define STATE_NOT_MATCH 0
#define STATE_MATCHING_NAGETIVE_NUM -1
#define STATE_MATCHING_POSITIVE_NUM 1
extern float gim_atof(char *buf, size_t len)
{
  char state = STATE_NOT_MATCH;
  float num = 0;
  size_t i = 0;
  for (; i < len; i++)
  {
    char l = buf[i];
    if (state == STATE_NOT_MATCH)
    {
      if (l == '-')
      {
        state = STATE_MATCHING_NAGETIVE_NUM;
        continue;
      }
      else if (l == '+')
      {
        state = STATE_MATCHING_POSITIVE_NUM;
        continue;
      }
      else if (l == '.')
      {
        state = STATE_MATCHING_POSITIVE_NUM;
        break;
      }
      else if (l >= '0' && l <= '9')
      {
        state = STATE_MATCHING_POSITIVE_NUM;
      }
      else
      {
        continue;
      }
    }
    else if (l < '0' || l > '9')
    {
      break;
    }
    num = (10 * num) + (l - '0');
  }
  if (i < len && buf[i] == '.')
  {
    ++i;
    float j = 0.1;
    for (; i < len; ++i)
    {
      char l = buf[i];
      if (l < '0' || l > '9')
      {
        break;
      }
      num += j * (l - '0');
      j *= 0.1;
    }
  }

  return state * num;
}
